import redis


class RedisHelper:

    def __init__(self, host, port, db, password):
        self.conn = redis.Redis(host=host, port=port, db=db, password=password, decode_responses=True)

    def setMapOneValue(self, name, key, value):
        self.conn.hset(name=name, key=key, value=value)

    def getMapValue(self, name, key):
        return self.conn.hget(name=name, key=key)

    def getEightMulitMapValue(self, name, key1, key2, key3, key4, key5, key6,key7,key8):
        return self.conn.hmget(name, key1, key2, key3, key4, key5, key6,key7,key8)

    def setMapValue(self, name, mapping):
        self.conn.hmset(name, mapping)
    def deleteExpireKeys(self,data_list):
        keys = self.conn.keys('*')
        for key in keys:
            TradingDay = redisHelper.getMapValue(key, 'TradingDay')
            if str(TradingDay) not in data_list: # data_list 需要保留的交易市场数据
                  self.conn.delete(key)



if __name__ == '__main__':
    import GlobalParam
    redisHelper = RedisHelper(GlobalParam.redis_ip, GlobalParam.redis_port, GlobalParam.redis_db,
                              GlobalParam.redis_password)
    #data_list = ("20240412", "20240415")
    #redisHelper.deleteExpireKeys(data_list)